#include "mat_ui_achievement_gain.h"
#include "mat_gamerule.h"
#include "mat_sfx.h"
#include "mat_sfx_manager.h"

#include "gameflow/ui_popup.h"
#include "application/game_application.h"
#include "application/game_rms.h"
#include "ui/ui_manager.h"


MATUIAchievementGain::MATUIAchievementGain() :	MATUIPopupAnimator("ui/achievement_gain.ui",""),
										m_State(STATE_COMING_NONE),
										m_pUIIcon(NULL),
										m_pUIMsg(NULL),
										m_Delay(0.0f),
										m_pSfx(NULL)
{
	if(m_pPopup)
	{
		MATUIPopupAnimatorMgr::Instance().AddPopupAnimator(MATUIPopupAnimatorMgr::POPUP_NAMES_ACHIEVEMENTGAIN,this);
		if(m_pPopup)
		{
			m_pAnimateWidget = m_pPopup->GetWidget("frame");
			m_pUIIcon = m_pPopup->GetWidget("icon");
			m_pUIMsg = m_pPopup->GetWidget("msg");
		}

        if(m_pAnimateWidget)
            m_pAnimateWidget->SetIsVisible(false);	// I dun use that now.
	}
	m_Type = ANIMATION_SLIDEFROMRIGHT;


	m_pUIAnimatedIcon = ui::UIManager::Instance().GetWidget("achievement_icon");
	m_pUIAnimatedTarget = ui::UIManager::Instance().GetWidget("icon");
}

void	MATUIAchievementGain::GainAchievement(RMS_TYPE Type) 
{
	if( MATGameRule::Instance().IsTutorialActive())
		return;

	if(g_pApp && g_pApp->GetRMS())
	{
		if(g_pApp->GetRMS()->GetInt(Type) == 0)
		{
			g_pApp->GetRMS()->SetInt(Type,1);
			
			char	Buffer[256];		    		
			sprintf(Buffer, "ui_img/icon_%i.png", ((int)Type)+1);

			if(m_pUIIcon)
				m_pUIIcon->SetOffImage(Buffer);

			sprintf(Buffer, "ACHIEVEMENT_%i", ((int)Type)+1);
			if(m_pUIMsg)
				m_pUIIcon->SetTextA(Buffer);

			m_QueueNotification.push_back(Type);			
		}
	}
}
	
bool	MATUIAchievementGain::IsGainedAchievement(RMS_TYPE Type) const
{
	if(g_pApp->GetRMS())
		return (g_pApp->GetRMS()->GetInt(Type) > 0);

	return false;
}

void	MATUIAchievementGain::Tick(float dt)
{
	if(m_QueueNotification.size() > 0)
	{
		this->Show();
		m_Delay = 2.2f;
		m_QueueNotification.erase(m_QueueNotification.begin());
	}

	if(m_Delay > 0)
	{
		m_Delay-= dt;
		if(m_Delay <= 0)
		{
			if(m_QueueNotification.size() == 0)
				this->Hide();
			else
			{
				char	Buffer[256];		    		
				RMS_TYPE Type = m_QueueNotification[0];
				sprintf(Buffer, "ui_img/icon_%i.png", ((int)Type)+1);

				if(m_pUIIcon)
					m_pUIIcon->SetOffImage(Buffer);

				sprintf(Buffer, "ACHIEVEMENT_%i", ((int)Type)+1);
				if(m_pUIMsg)
					m_pUIIcon->SetTextA(Buffer);

				m_Delay = 1.5f;
			}
		}
	}

	if(m_pUIAnimatedIcon && m_pSfx && m_pUIAnimatedIcon->GetPosition().x < 2.0f)
	{
		m_pSfx->FollowMe(m_pUIAnimatedIcon->GetPosition());
	}


}

void	MATUIAchievementGain::ForceFinish()
{
	m_QueueNotification.resize(0);
	this->Hide();
}

void	MATUIAchievementGain::Hide()
{
	MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TORIGHT,0, this);

	if(m_pUIAnimatedIcon && m_pUIIcon && m_pUIAnimatedTarget)
	{
		//m_pUIAnimatedIcon->SetOffImage(m_pUIIcon->GetImageTextFromState(ui::UIWidget::WI_MOUSE_OFF));
		m_pUIAnimatedIcon->SetPositionRelativeToParent(m_pUIIcon->GetPosition());
		MATAnimatedUI* pAnimatedUI = MATUIAnimator::Instance().Add(m_pUIAnimatedIcon,MATAnimatedUI::ANIMATE_STYLE_CENTERSCREEN,0, this);
		((MATAnimatedUIMove*)pAnimatedUI)->SetTargetPosition(m_pUIAnimatedTarget->GetPosition());

		m_pSfx = MATSFXManager::Instance().Spawn(
											MATSfx::SFX_TROPHY_ANIMATE,
											m_pUIAnimatedIcon->GetPosition(),
											1.0f,0,false,MATSounds::SFX_NONE
											);
		if(m_pSfx)
			m_pSfx->SetIsPostRender(true);

	}
	
}

void	MATUIAchievementGain::OnAnimatedUIFinish()
{
	if(m_pPopup && m_pAnimateWidget->GetPosition().x >= 1.0f)
	{
		m_pPopup->Hide(NULL);
		//if(g_pApp && g_pApp->GetRMS()) 
		//	g_pApp->GetRMS()->Save();
	}

	if(m_pUIAnimatedIcon && m_pUIAnimatedTarget)
	{
		const math::Vec2 Temp = m_pUIAnimatedTarget->GetPosition() - m_pUIAnimatedIcon->GetPosition();
		if(Temp.Magnitude() < 0.05f)
		{
			m_pUIAnimatedIcon->SetPosition(math::Vec2(2.0f,2.0f));
			m_pSfx = NULL;
			MATUIAnimator::Instance().Add(m_pUIAnimatedTarget,MATAnimatedUI::ANIMATE_STYLE_POPPINGUP,0,NULL);
		}
	}
}
